package org.mat.ipaas.ucenter.service.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.biz.common.constant.CommonConstant;
import org.mat.ipaas.ucenter.biz.common.system.query.QueryGenerator;
import org.mat.ipaas.ucenter.biz.common.util.oConvertUtils;
import org.mat.ipaas.ucenter.biz.modules.system.entity.SysDataLog;
import org.mat.ipaas.ucenter.biz.modules.system.service.ISysDataLogService;
import org.mat.ipaas.ucenter.service.api.system.SysDataLogRestAPI;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysDataLogDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统数据日志
 */
@RestController
@RequestMapping("/sys/dataLog")
@Slf4j
public class SysDataLogController implements SysDataLogRestAPI {

    @Autowired
    private ISysDataLogService service;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MatApiResponse<IPage<SysDataLogDTO>> queryPageList(SysDataLogDTO sysDataLogDTO, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        MatApiResponse<IPage<SysDataLogDTO>> matApiResponse = new MatApiResponse<>();

        SysDataLog sysDataLog = toSysDataLog(sysDataLogDTO);

        sysDataLog.setType(CommonConstant.DATA_LOG_TYPE_JSON);
        QueryWrapper<SysDataLog> queryWrapper = QueryGenerator.initQueryWrapper(sysDataLog, req.getParameterMap());
        Page<SysDataLog> page = new Page<SysDataLog>(pageNo, pageSize);
        IPage<SysDataLog> pageList = service.page(page, queryWrapper);
        log.info("查询当前页：" + pageList.getCurrent());
        log.info("查询当前页数量：" + pageList.getSize());
        log.info("查询结果数量：" + pageList.getRecords().size());
        log.info("数据总数：" + pageList.getTotal());
        matApiResponse.setSuccess(true);
        matApiResponse.setData(toSysDataLogDTOPage(pageList));
        return matApiResponse;
    }

    /**
     * 查询对比数据
     *
     * @param req
     * @return
     */
    @RequestMapping(value = "/queryCompareList", method = RequestMethod.GET)
    public MatApiResponse<List<SysDataLogDTO>> queryCompareList(HttpServletRequest req) {
        MatApiResponse<List<SysDataLogDTO>> matApiResponse = new MatApiResponse<>();
        String dataId1 = req.getParameter("dataId1");
        String dataId2 = req.getParameter("dataId2");
        List<String> idList = new ArrayList<String>();
        idList.add(dataId1);
        idList.add(dataId2);
        try {
            List<SysDataLog> list = (List<SysDataLog>) service.listByIds(idList);
            matApiResponse.setData(toSysDataLogDTOList(list));
            matApiResponse.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return matApiResponse;
    }

    /**
     * 查询版本信息
     *
     * @param req
     * @return
     */
    @RequestMapping(value = "/queryDataVerList", method = RequestMethod.GET)
    public MatApiResponse<List<SysDataLogDTO>> queryDataVerList(HttpServletRequest req) {
        MatApiResponse<List<SysDataLogDTO>> matApiResponse = new MatApiResponse<>();
        String dataTable = req.getParameter("dataTable");
        String dataId = req.getParameter("dataId");
        QueryWrapper<SysDataLog> queryWrapper = new QueryWrapper<SysDataLog>();
        queryWrapper.eq("data_table", dataTable);
        queryWrapper.eq("data_id", dataId);
        //update-begin-author:taoyan date:2022-7-26 for: 新增查询条件-type
        String type = req.getParameter("type");
        if (oConvertUtils.isNotEmpty(type)) {
            queryWrapper.eq("type", type);
        }
        // 按时间倒序排
        queryWrapper.orderByDesc("create_time");
        //update-end-author:taoyan date:2022-7-26 for:新增查询条件-type

        List<SysDataLog> list = service.list(queryWrapper);
        if (list == null || list.size() <= 0) {
            matApiResponse.fail("未找到版本信息");
        } else {
            matApiResponse.setData(toSysDataLogDTOList(list));
            matApiResponse.setSuccess(true);
        }
        return matApiResponse;
    }


    private IPage<SysDataLogDTO> toSysDataLogDTOPage(IPage<SysDataLog> pageList) {
        Page<SysDataLogDTO> sysDataLogDTOPage = new Page<>();
        List<SysDataLogDTO> sysDataLogDTOList = new ArrayList<>();
        if (pageList != null && CollectionUtils.isNotEmpty(pageList.getRecords())) {
            for (SysDataLog sysDataLog : pageList.getRecords()) {
                sysDataLogDTOList.add(toSysDataLogDTO(sysDataLog));
            }
        }

        sysDataLogDTOPage.setTotal(pageList.getTotal());
        sysDataLogDTOPage.setSize(pageList.getSize());
        sysDataLogDTOPage.setCurrent(pageList.getCurrent());
        sysDataLogDTOPage.setRecords(sysDataLogDTOList);
        return sysDataLogDTOPage;
    }

    private SysDataLogDTO toSysDataLogDTO(SysDataLog sysDataLog) {
        if (sysDataLog == null) {
            return null;
        }
        SysDataLogDTO sysDataLogDTO = new SysDataLogDTO();
        sysDataLogDTO.setId(sysDataLog.getId());
        sysDataLogDTO.setCreateBy(sysDataLog.getCreateBy());
        sysDataLogDTO.setCreateTime(sysDataLog.getCreateTime());
        sysDataLogDTO.setUpdateBy(sysDataLog.getUpdateBy());
        sysDataLogDTO.setUpdateTime(sysDataLog.getUpdateTime());
        sysDataLogDTO.setDataTable(sysDataLog.getDataTable());
        sysDataLogDTO.setDataId(sysDataLog.getDataId());
        sysDataLogDTO.setDataContent(sysDataLog.getDataContent());
        sysDataLogDTO.setDataVersion(sysDataLog.getDataVersion());
        sysDataLogDTO.setType(sysDataLog.getType());
        return sysDataLogDTO;
    }

    private SysDataLog toSysDataLog(SysDataLogDTO sysDataLogDTO) {
        if (sysDataLogDTO == null) {
            return null;
        }
        SysDataLog sysDataLog = new SysDataLog();
        sysDataLog.setId(sysDataLogDTO.getId());
        sysDataLog.setCreateBy(sysDataLogDTO.getCreateBy());
        sysDataLog.setCreateTime(sysDataLogDTO.getCreateTime());
        sysDataLog.setUpdateBy(sysDataLogDTO.getUpdateBy());
        sysDataLog.setUpdateTime(sysDataLogDTO.getUpdateTime());
        sysDataLog.setDataTable(sysDataLogDTO.getDataTable());
        sysDataLog.setDataId(sysDataLogDTO.getDataId());
        sysDataLog.setDataContent(sysDataLogDTO.getDataContent());
        sysDataLog.setDataVersion(sysDataLogDTO.getDataVersion());
        sysDataLog.setType(sysDataLogDTO.getType());
        return sysDataLog;
    }

    private List<SysDataLogDTO> toSysDataLogDTOList(List<SysDataLog> list) {
        List<SysDataLogDTO> sysDataLogDTOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            for (SysDataLog sysDataLog : list) {
                sysDataLogDTOList.add(toSysDataLogDTO(sysDataLog));
            }
        }
        return sysDataLogDTOList;
    }
}
